package com.beerandjesus.sportofkings.handicapper.factory.profile;

import com.beerandjesus.sportofkings.handicapper.model.*;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.HorseRace;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Race;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformance;
import com.beerandjesus.sportofkings.handicapper.strategy.CramerJockeyRatingResolver;
import com.beerandjesus.sportofkings.handicapper.dao.Database;

import java.util.List;

/**
 * This profile is characterized by a drop in class from the previous race coupled
 * with a switch to a better jockey or another jockey of the same quality, as
 * described by Marc Cramer.
 * Date: Jul 6, 2012
 * Time: 3:46:46 PM
 */
public class CramerAnalysisProfileResolver implements AnalysisProfileResolver {

    private Database database;

    public AnalysisProfile resolve(HorseRace race, Horse horse) {
        // Must be a drop in class
        Race r = race.getRace();
        RaceType type = r.getType();
        if (type == null) {
            return null;
        }
        List<PastPerformance> pps = horse.getPastPerformances();
        if (pps == null || pps.size() == 0) {
            return null;
        }
        PastPerformance pp = pps.get(0);
        if (pp == null) {
            return null;
        }
        RaceType lastType = pp.getRaceType();
        if (lastType == null) {
            return null;
        }
        Integer claimingPrice = null;
        Integer lastClaimingPrice = null;
        if (!type.isCramerClassDrop(claimingPrice, lastType, lastClaimingPrice)) {
            return null;
        }
        AnalysisProfile prof = AnalysisProfile.getAnalysisProfile(AnalysisProfileType.CRAMER_DROP_IN_CLASS_POSS_JOCKEY_SWITCH, 0);
        // Must be a jockey switch, either to a higher class jockey or
        // another jockey of the same class
        // However, can be the same jockey if the jockey won the last race
        TrainerJockeyOwner tjo = horse.getTrainerJockeyOwner();
        if (tjo == null) {
            return prof;
        }
        String jock = tjo.getJockeyName();
        String lastJock = pp.getJockeyName();
        if (jock == null || lastJock == null) {
            return prof;
        }
        if (jock.equals(lastJock)) {
            // No switch. Profile only applies if the jockey won the last race
            String finPos = pp.getFinishPosition();
            int pos = 0;
            try {
                pos = Integer.valueOf(finPos);
            } catch (NumberFormatException e) {
                // Ignore
            }
            if (pos == 1) {
                return AnalysisProfile.getAnalysisProfile(AnalysisProfileType.CRAMER_DROP_IN_CLASS_WITH_JOCKEY_SWITCH, 0);
            }
            return null;
        }
        JockeyRating rating = getDatabase().getJockeyRatingByName(jock);
        if (rating == null) {
            return prof;
        }
        JockeyRating lastRating = getDatabase().getJockeyRatingByName(lastJock);
        if (lastRating == null) {
            return prof;
        }
        if (rating.getRating().ordinal() >= lastRating.getRating().ordinal()) {
            return AnalysisProfile.getAnalysisProfile(AnalysisProfileType.CRAMER_DROP_IN_CLASS_WITH_JOCKEY_SWITCH, 0);
        }
        return null;
    }

    public Database getDatabase() {
        return database;
    }

    public void setDatabase(Database database) {
        this.database = database;
    }
}
